/**
 * Created by cyf on 15-12-1.
 * 群组设置窗口
 */
var qxchat;
var groupId;
var baseUrl = "http://" + global.serverPath + ":" + global.serverPort;
//初始化
$(function() {
    new qxchat.WindowSize();
    new qxchat.GroupManager();
});
/**
 * Created by cyf on 15-12-1.
 * 群组的操作
 */
(function() {
    qxchat.GroupManager = GroupManager;
    var EXTEND = null; //是否需要继承
    function GroupManager() {
        if (EXTEND) {
            EXTEND.apply(this, arguments);
        }
        var self = this;
        this.init();
        //关闭
        $("#cancel").bind('click', self.closeWin);
        //应用
        $("#apply").bind('click', self.saveInfo);
        //解散事件
        $("#btn_dissolve").bind('click', self.dissolve);
        //邀请事件
        $("#btn_inviteMember").bind('click', self.inviteMember);
    }
    GroupManager.prototype = {
        constructor: GroupManager,
        init: function() {
            //加载组数据
            if (groupId != null) {
                layer.load(0, {
                    shade: false
                });
                $.ajax({
                    type: 'post',
                    url: baseUrl + "/group/view",
                    dataType: "json",
                    data: {
                        groupId: groupId
                    },
                    success: function(data) {
                        $("#group_id").val(data.group.id);
                        $("#group_name").val(data.group.name);
                        $("#group_intro").val(data.group.intro);
                        $.each(data.members, function(index, domEle) {
                            var str = "<tr><td>" + domEle.nickname + "</td><td>" + domEle.online + "</td></tr>";
                            $("#grid-group-members tbody").append(str);
                        });
                        layer.closeAll('loading');
                    }
                });
            } else {
                $("#btn_dissolve").css("display", "none");
                $("#tab_member").css("display", "none");
                var str = "<tr><td>" + global.currentUserName + "</td><td>" + 1 + "</td></tr>";
                $("#grid-group-members tbody").append(str);
            }
        },
        dissolve: function() {
            //询问框
            layer.confirm('您确定要解散群组？', {
                btn: ['确定', '不要'] //按钮
            }, function() {
                $.ajax({
                    type: 'post',
                    url: baseUrl + "/group/dissolve",
                    dataType: "json",
                    data: {
                        groupId: groupId
                    },
                    success: function(data) {
                        var tmpTreeObj = global.groupDataTreeObj.zTreeObj;
                        tmpTreeObj.removeNode(tmpTreeObj.getNodeByParam("id", groupId));
                        layer.msg('群组已解散,窗口即将关闭', {
                            btn: ['立即关闭']
                        }, function() {
                            var _gui = require('nw.gui');
                            var _win = _gui.Window.get();
                            _win.close();
                        });
                    }
                });
            }, function() {});
        },
        saveInfo: function() {
            var options = {
                data: {
                    ownerLoginName: global.currentUserLoginName
                },
                url: baseUrl + "/group/save",
                type: "post",
                dataType: "json",
                success: function(data) {
                    groupId = data.id;
                    $("#group_id").val(data.id);
                    $("#btn_dissolve").css("display", "inline");
                    $("#tab_member").css("display", "inline");
                    var tmpTreeObj = global.groupDataTreeObj.zTreeObj;
                    var node = tmpTreeObj.getNodeByParam("id", data.id);
                    if (node == null) {
                        var newNode = {
                            id: data.id,
                            name: data.name,
                            click: true,
                            pId: null,
                            isParent: false
                        };
                        tmpTreeObj.addNodes(null, newNode)[0];
                    } else {
                        node.name = data.name;
                        tmpTreeObj.updateNode(node);
                    }
                    layer.msg('保存成功', {
                        icon: 1
                    });
                }
            };
            $('#groupForm').ajaxSubmit(options);
        },
        closeWin: function() {
            var _gui = require('nw.gui');
            var _win = _gui.Window.get();
            _win.close();
        },
        inviteMember: function() {
            $.ajax({
                type: 'post',
                url: baseUrl + "/group/getOrgTree",
                dataType: "json",
                success: function(data) {
                    var setting = {
                        check: {
                            enable: true
                        },
                        data: {
                            simpleData: {
                                enable: true
                            }
                        },
                        callback: {
                            onCheck: onCheck
                        }
                    };
                    function onCheck(e, treeId, treeNode) {
                        var zTree = $.fn.zTree.getZTreeObj("orgTreeView"),
                        nodes = zTree.getCheckedNodes(true),
                        v = "";
                        for (var i=0, l=nodes.length; i<l; i++) {
                            if(nodes[i].isParent==false){
                                v += nodes[i].id + ";";
                            }
                        }
                        if (v.length > 0 ) v = v.substring(0, v.length-1);
                        $("#memberIds").val(v);
                    }
                    $.fn.zTree.init($("#orgTreeView"), setting, data);
                    layer.open({
                        type: 1,
                        area: '260px',
                        content: $('#div_tree'),
                        btn: ['添加'],
                        btn1: function(index, layero){
                            $.ajax({
                                type: 'post',
                                url: baseUrl + "/group/addMembers",
                                dataType: "json",
                                data: {
                                    groupId: groupId,
                                    memberIds: $("#memberIds").val()
                                },
                                success: function(data) {
                                    $.each(data.members, function(index, domEle) {
                                        var str = "<tr><td>" + domEle.nickname + "</td><td>" + domEle.online + "</td></tr>";
                                        $("#grid-group-members tbody").append(str);
                                    });
                                }
                            });
                        }
                    });
                }
            });
        }
    }
})();
/**
 * Created by cyf on 15-12-1.
 * 初始化窗口的三个按钮
 */
(function() {
    qxchat.WindowSize = WindowSize;
    var EXTEND = null; //是否需要继承
    function WindowSize() {
        if (EXTEND) {
            EXTEND.apply(this, arguments);
        }
        this._sizeFlag = true;
        this._gui = require('nw.gui');
        this._win = this._gui.Window.get();
        this.init();
        var self = this;
        $('.min').bind('click', function() {
            self.minimize();
        });
        $('.max').bind('click', function() {
            self.maxToggle();
        });
        $('.close').bind('click', function() {
            self.close();
        });
        $('.close-chat').click(function() {
            self.close();
        })
        $(document).keydown(function(event) {
            if (event.altKey && event.keyCode == 67) {
                self.close();
            };
            if (event.altKey && event.keyCode == 83) {
                sendMessage();
            };
        });

    }
    WindowSize.prototype = {
        constructor: WindowSize,
        init: function() {

        },
        minimize: function() {
            this._win.minimize();
            this._sizeFlag = true;
        },
        maxToggle: function() {
            if (this._sizeFlag) {
                this._win.maximize();
                this._sizeFlag = false;
            } else {
                this._win.unmaximize();
                this._sizeFlag = true;
            }
        },
        close: function() {
            this._win.close();
        }
    }
})();